package com.chx.starter.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;

public class OssCondition implements Condition {

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        String endpoint = conditionContext.getEnvironment().getProperty("oss.endpoint");
        String accessKeyId = conditionContext.getEnvironment().getProperty("oss.accessKeyId");
        String accessKeySecret = conditionContext.getEnvironment().getProperty("oss.accessKeySecret");
        String bucketName = conditionContext.getEnvironment().getProperty("oss.bucketName");
        if (StringUtils.isEmpty(endpoint)) {
            throw new RuntimeException("Lack of aliyun.oss configuration:oss.endpoint");
        } else if (StringUtils.isEmpty(accessKeyId)) {
            throw new RuntimeException("Lack of aliyun.oss configuration:oss.accessKeyId");
        } else if (StringUtils.isEmpty(accessKeySecret)) {
            throw new RuntimeException("Lack of aliyun.oss configuration:oss.accessKeySecret");
        } else if (StringUtils.isEmpty(bucketName)) {
            throw new RuntimeException("Lack of aliyun.oss configuration:oss.bucketName");
        } else {
            return true;
        }
    }
}
